
function refereshDrugListGrid(){
    var lib = new DojoLib();
    lib.bindGridStore('services/manage_drug/retrieve_drug_list.php', '', 'drugListGrid');
}

function addDrugItem(){
    openDrugItemFormDialog('รายละเอียดตัวยา', 'เพิ่มยา', function(){}, function(){
        var obj = new Generic();
        obj.set('drug_name', dijit.byId('drugName').get('value'));
        obj.set('drug_detail', dijit.byId('drugDetail').get('value'));
        
        var lib = new DojoLib();
        lib.postJson('services/manage_drug/add_drug_item.php', obj.toJson(), function(res){
            refereshDrugListGrid();
            dijit.byId('drugItemFormDialog').hide();
        });
    });
}

function openEditDrugItemDialog(id){
    openDrugItemFormDialog('รายละเอียดตัวยา', 'แก้ไข', function(){
        var obj = new Generic();
        obj.set('drug_id', id);
        
        var lib = new DojoLib();
        lib.postJson('services/manage_drug/get_drug_table.php', obj.toJson(), function(res){
            //alert(res);
            document.getElementById('drugId').value = res.drug_id;
            dijit.byId('drugName').set('value', res.drug_name);
            dijit.byId('drugDetail').set('value', res.drug_detail);
        });
    }, function(){
        var obj = new Generic();
        obj.set('drug_id', document.getElementById('drugId').value);
        obj.set('drug_name', dijit.byId('drugName').get('value'));
        obj.set('drug_detail', dijit.byId('drugDetail').get('value'));
        
        var lib = new DojoLib();
        lib.postJson('services/manage_drug/update_drug_item.php', obj.toJson(), function(res){
            refereshDrugListGrid();
            dijit.byId('drugItemFormDialog').hide();
        });
    });
}

function checkDrugItemToDelete(id){
    alert('ตรวจสอบว่ายาถูกอ้างอิงอยู่หรือป่าว')
}

function openDrugItemFormDialog(title, okBtnLabel, handleOnLoadDialog, handleOkBtn){
    var drugItemFormDialog = new dijit.Dialog({
        title : title,
        id : "drugItemFormDialog",
        style : "width:600px; height:240px;",
        href : "htmls/manage_drug/drug_form.html",
        onHide : function() {
            this.destroyRecursive();
        },
        onLoad : function() {
            dijit.byId('okBtn').set('label', okBtnLabel);
            dijit.byId('okBtn').set('onClick', function(){
                handleOkBtn();
            });
            dijit.byId('cancelBtn').set('onClick', function(){
                drugItemFormDialog.hide();
            });
            handleOnLoadDialog();
        }
    });
    drugItemFormDialog.show();
}